import express from "express";
import { formatRequestQuery, formatResponse } from "../../utils/tools";
import { MessageListService } from "../../services/HomePage/MessageListService";
const router = express.Router();

router.post("/", async (req, res) => {
  await MessageListService.add(req.body);
  res.send(formatResponse<null>(null, "操作成功"));
});

router.delete("/:id", async (req, res) => {
  const result = await MessageListService.delete(req.params.id);
  res.send(formatResponse(result));
});

router.put("/", async (req, res) => {
  const result = await MessageListService.update(req.body);
  res.send(formatResponse(result));
});

router.get("/page", async (req, res) => {
  const { query, other } = formatRequestQuery(req.query, []);
  const result = await MessageListService.findByPage(query.current, query.size, other);
  res.send(formatResponse<typeof result>(result));
});

router.get("/list", async (req, res) => {
  const result = await MessageListService.findByList(req.query);
  res.send(formatResponse<typeof result>(result));
});

router.get("/treeList", async (req, res) => {
  const { other } = formatRequestQuery(req.query, []);
  const result = await MessageListService.findByTreeList(other);
  res.send(formatResponse<typeof result>(result));
});

router.get("/:id", async (req, res) => {
  const result = await MessageListService.findById(req.params.id);
  res.send(formatResponse(result));
});

export default router;
